package com.rrqs.activity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.rrqs.activity.common.Common;
import com.rrqs.activity.common.Const;
import com.rrqs.activity.data.OrderNewHelpData;
import com.rrqs.views.CountDownTimer;
import com.rrqs.views.OnChanged;
import com.zadtek.xmqishi.R;

import java.util.List;

/**
 * 新任务列表
 *
 * @author kywwl
 */
public class OrderNewHelpAdapter extends CommonAdapter<OrderNewHelpData> {
    private OnChanged listener;
    private SparseArray<CountDownTimer> timers;

    public OrderNewHelpAdapter(Context context, List<OrderNewHelpData> datas,
                               int layoutId) {
        super(context, datas, layoutId);
        timers = new SparseArray<>();
        // TODO Auto-generated constructor stub
    }

    @SuppressLint("NewApi")
    @Override
    public void convert(final ViewHolders holder, final OrderNewHelpData t,int position) {
        ImageView newtask_order_img = holder.getView(R.id.newtask_order_img);
        TextView newtaskmomey = holder.getView(R.id.newtask_money);
        TextView newtasktime = holder.getView(R.id.newtask_time);
        LinearLayout show_layout = holder.getView(R.id.show_layout);

        TextView newtask_goods_name = holder.getView(R.id.newtask_goods_name);
        TextView newtask_goods_address = holder.getView(R.id.newtask_goods_address);
        TextView help_goods_distance = holder.getView(R.id.help_goods_distance);
        TextView newtask_goods_distance = holder.getView(R.id.newtask_goods_distance);
        TextView newtask_goods_getaddress = holder
                .getView(R.id.newtask_goods_getaddress);
        Button submit = holder.getView(R.id.submit);

        newtask_goods_name.setText(t.getTitle());
        newtask_goods_address.setText(t.getGet_address());
        LinearLayout newhelp_layout = holder.getView(R.id.newhelp_layout);
        if (t.getGet_address().length() > 0) {
            newtask_goods_address.setVisibility(View.VISIBLE);
        } else {
            newtask_goods_address.setVisibility(View.GONE);
        }
        if (t.getType().equals("buy")) {
            newtask_order_img.setImageResource(R.drawable.gou);
            submit.setText("帮你买");
        } else if (t.getType().equals("get")) {
            newtask_order_img.setImageResource(R.drawable.getgoods);
            submit.setText("帮你取");
        } else if (t.getType().equals("send")) {
            newtask_order_img.setImageResource(R.drawable.fa);
            submit.setText("帮你送");
        }

        LinearLayout juli_layout = holder.getView(R.id.juli_layout);//送货距离
        LinearLayout juli_layout01 = holder.getView(R.id.juli_layout01);//取货距离
        if (!t.getLocation_get().equals("0")) {
            help_goods_distance.setText(t.getLocation_get() + "Km");
            juli_layout01.setVisibility(View.VISIBLE);
        } else {
            juli_layout01.setVisibility(View.GONE);
        }
        if (!t.getLocation_send().equals("0")) {
            newtask_goods_distance.setText(t.getLocation_send() + "Km");
            juli_layout.setVisibility(View.VISIBLE);
        } else {
            juli_layout.setVisibility(View.GONE);
        }
        newtask_goods_getaddress.setText(t.getSend_address());

        TextView getgpsaddr = holder.getView(R.id.getgpsaddr);
        newtaskmomey.setText("¥" + t.getPercentage());
        if (!t.getHelp_addtime().equals("") || t.getHelp_addtime() != null) {
            newtasktime.setText(Common.getStrTime(t.getHelp_addtime()));
        }

        try {
            String address_road = t.getMap_location();
            if (address_road == null || address_road.equals("") || address_road.equals("null")) {
                getgpsaddr.setVisibility(View.GONE);
            } else {
                getgpsaddr.setText(address_road); // 根据经纬度反查地址
                getgpsaddr.setVisibility(View.VISIBLE);
            }
        } catch (Exception e) {
        }


        submit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                SharedPreferences sp = mContext.getSharedPreferences(
                        Const.PREF_NAME, Context.MODE_PRIVATE);
                String Uid = sp.getString("uid", "");
                if (null != Uid && !Uid.equals("")) {
                    if (t.getType().equals("buy")) {
                        listener.getHelpChoiceData(t.getHelp_order_id(), Uid, holder.getPositon(), "1");
                    } else if (t.getType().equals("get")) {
                        listener.getHelpChoiceData(t.getHelp_order_id(), Uid, holder.getPositon(), "3");
                    } else if (t.getType().equals("send")) {
                        listener.getHelpChoiceData(t.getHelp_order_id(), Uid, holder.getPositon(), "2");
                    }

                }
            }
        });
        //时间计时器相关
        TextView orderStartTime = holder.getView(R.id.orderStartTime);
        TextView orderEndTime = holder.getView(R.id.orderEndTime);
        TextView order_end_time_stamp = holder.getView(R.id.order_end_time_stamp);
        ProgressBar progress = holder.getView(R.id.progress);
        orderStartTime.setText(t.getOrder_start_time());
        orderEndTime.setText(t.getOrder_end_time());
        progress.setProgress((int) (0.8 * 100));


        int order_end_time_stamp1 = t.getOrder_end_time_stamp();
        //获取当前时间戳
//        long timeStamp = System.currentTimeMillis() / 1000;
        long timeStamp = 0;
        CountDownTimer countDownTimer = timers.get(order_end_time_stamp.hashCode());
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }

        long s1 = order_end_time_stamp1 - timeStamp;
        long s2 = timeStamp - order_end_time_stamp1;
        if (s1 > 0) {
            countdown(s1, order_end_time_stamp, false, s2);
        } else {
//			if (s1 + 36000 > 0) {
//				countdown((s1 + 36000), order_end_time_stamp, true, s2);
//			} else {
            order_end_time_stamp.setText("已超时");
//			}
        }

        if (!t.getAdd_time().equals("") || t.getAdd_time() != null) {
            newtasktime.setText(Common.getStrTime(t.getAdd_time()));
        }
        //新增字段相关
        TextView tv_code = holder.getView(R.id.number);
        TextView tv_type = holder.getView(R.id.type);
        TextView tv_phone = holder.getView(R.id.phone);
        tv_code.setText("商家流水号："+t.getLiushui());
        if(t.getDingdantype()==1){
            tv_type.setText("订单类型：系统派单");
        }else{
            tv_type.setText("订单类型：人工派单");
        }
        tv_type.setVisibility(View.INVISIBLE);
        tv_phone.setText("顾客手机号："+t.getUser_phone());
    }

    public void setHelpOnChanged(OnChanged listener) {
        this.listener = listener;
    }
    private void countdown(final long s, final TextView tv, final boolean isTimeOut, final long s2) {
        /** 倒计时s秒，一次1秒 */
        CountDownTimer timer = new CountDownTimer(s * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub
                long ss;
                if (isTimeOut) {
                    ss = s2 + s - millisUntilFinished / 1000;
                } else {
                    ss = millisUntilFinished / 1000;
                }
                if (ss < 60) {
                    if (isTimeOut) {
                        tv.setText("超时" + ss + "秒");
                    } else {
                        tv.setText("剩余" + ss + "秒");
                    }
                } else if (millisUntilFinished < 3600) {
                    if (isTimeOut) {
                        tv.setText("超时" + ss / 60 + "分" + ss % 60 + "秒");
                    } else {
                        tv.setText("剩余" + ss / 60 + "分" + ss % 60 + "秒");
                    }
                } else {
                    if (isTimeOut) {
                        tv.setText("超时" + ss / 3600 + "时" + ss % 3600 / 60 + "分" + ss % 60 + "秒");
                    } else {
                        tv.setText("剩余" + ss / 3600 + "时" + ss % 3600 / 60 + "分" + ss % 60 + "秒");
                    }
                }
            }

            @Override
            public void onFinish() {
                if (isTimeOut) {
                    tv.setText("已超时");
                } else {
                    countdown(36000, tv, true, 0);
                }
            }
        }.start();

        timers.put(tv.hashCode(), timer);
    }
}
